﻿using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;

namespace Itk.Fields
{
    public class OrderFieldControl : BaseFieldControl
    {
        protected TextBox _oTxtValue;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if (ControlMode == SPControlMode.Edit || ControlMode == SPControlMode.New)
            {
                if (!Page.ClientScript.IsClientScriptIncludeRegistered("itk_js_utils"))
                    Page.ClientScript.RegisterClientScriptInclude("itk_js_utils", "/_layouts/ITK/ITK.Utilities.js");

                _oTxtValue = new TextBox { CssClass = "ms-input", MaxLength = 10 };
                _oTxtValue.Attributes.Add("onkeydown", "return numericEntry(event);");

                if (ControlMode == SPControlMode.New) 
                    _oTxtValue.Text = (List.ItemCount + 1).ToString();

                Controls.Add(_oTxtValue);
            }
        }

        //Returns or sets the actual field value object from the page data
        public override object Value
        {
            get
            {
                EnsureChildControls();
                return _oTxtValue.Text;
            }
            set
            {
                EnsureChildControls();
                if (value == null) return;

                _oTxtValue.Text = value.ToString();
            }
        }
    }
}
